home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / kernel.zip / K1.C < prev    next >
Text File  |  1986-11-25  |  28KB  |  750 lines

  1. /*
  2. Listing 1      Scheduling Algorithm
  3. (C) Copyright 1986 Ken Berry.
  4. All rights reserved.
  5. Copies may be made for non-commercial, private use only.
  6. */
  7.  
  8. #define _F 0         /* false */
  9. #define _T 1         /* true */
  10. #define _E -1         /* error */
  11.  
  12. #define _NULL 0      /* null pointer */
  13.  
  14. typedef char pointer;     /* pointer type */
  15. typedef char logical;     /* logical type */
  16. typedef unsigned selector; /* 8086 selector type */
  17.  
  18. struct sys_parm      /* register storage block for 8086 interface */
  19. {
  20.   union {unsigned sys_rax; struct {char sys_ral, sys_rah;} sys_byt;} sys_ra;
  21.   union {unsigned sys_rbx; struct {char sys_rbl, sys_rbh;} sys_byt;} sys_rb;
  22.   union {unsigned sys_rcx; struct {char sys_rcl, sys_rch;} sys_byt;} sys_rc;
  23.   union {unsigned sys_rdx; struct {char sys_rdl, sys_rdh;} sys_byt;} sys_rd;
  24. #define sys_ax sys_ra.sys_rax
  25. #define sys_al sys_ra.sys_byt.sys_ral
  26. #define sys_ah sys_ra.sys_byt.sys_rah
  27. #define sys_bx sys_rb.sys_rbx
  28. #define sys_bl sys_rb.sys_byt.sys_rbl
  29. #define sys_bh sys_rb.sys_byt.sys_rbh
  30. #define sys_cx sys_rc.sys_rcx
  31. #define sys_cl sys_rc.sys_byt.sys_rcl
  32. #define sys_ch sys_rc.sys_byt.sys_rch
  33. #define sys_dx sys_rd.sys_rdx
  34. #define sys_dl sys_rd.sys_byt.sys_rdl
  35. #define sys_dh sys_rd.sys_byt.sys_rdh
  36.   unsigned sys_bp;     /* base pointer */
  37.   unsigned sys_si;     /* source index */
  38.   unsigned sys_di;     /* destination index */
  39.   unsigned sys_sp;     /* stack pointer */
  40.   unsigned sys_cs;     /* code segment */
  41.   unsigned sys_ds;     /* data segment */
  42.   unsigned sys_ss;     /* stack segment */
  43.   unsigned sys_es;     /* extra segment */
  44.   unsigned sys_pf;     /* 80286 processor flags */
  45. #define SYS_OF 0x0800          /* overflow flag- 1: lost significance */
  46. #define SYS_DF 0x0400          /* direction flag- 1: strings auto-decrement */
  47. #define SYS_IF 0x0200          /* interrupt flag- 1: enable interrupts */
  48. #define SYS_TF 0x0100          /* trap flag- 1: interrupt every instruction */
  49. #define SYS_SF 0x0080          /* sign flag- 1: result negative */
  50. #define SYS_ZF 0x0040          /* zero flag- 1: result 0 */
  51. #define SYS_AF 0x0010          /* auxiliary carry flag- 1: carry from bit 3 */
  52. #define SYS_PF 0x0004          /* parity flag- 1: even number of 1's */
  53. #define SYS_CF 0x0001          /* carry flag- 1: carry from bit 8 or 16 */
  54.   unsigned sys_sw;     /* status word */
  55. #define SYS_TS 0x0008          /* task switch */
  56. #define SYS_EM 0x0004          /* processor extension emulation */
  57. #define SYS_MP 0x0002          /* monitor processor extension */
  58. #define SYS_PE 0x0001          /* protection enable */
  59.   unsigned sys_ip;     /* instruction pointer */
  60.   unsigned sys_res;     /* unused */
  61. };
  62.  
  63. struct t_xstck
  64. {
  65.   unsigned t_xbase;     /* application stack base (overflow detection) */
  66.   unsigned t_xes;     /* es */
  67.   unsigned t_xbp;     /* bp */
  68.   unsigned t_xdi;     /* di */
  69.   unsigned t_xsi;     /* si */
  70.   unsigned t_xdx;     /* dx */
  71.   unsigned t_xcx;     /* cx */
  72.   unsigned t_xbx;     /* bx */
  73.   unsigned t_xax;     /* ax */
  74.   unsigned t_xds;     /* ds */
  75.   unsigned t_xip;     /* ip */
  76.   unsigned t_xcs;     /* cs */
  77.   unsigned t_xpf;     /* pf */
  78.   unsigned t_retip;     /* return address */
  79. };
  80.  
  81. struct t_task
  82. {
  83.   char t_type;         /* task type */
  84. #define T_X 0x80     /* execute queue */
  85. #define T_W 0x40     /* wait queue */
  86. #define T_P 0x20     /* priority queue */
  87. #define T_SW 0x10     /* secondary wait queue */
  88. #define T_ATASK 0x01     /* abreviated task */
  89.   unsigned t_wttk;     /* wait tick count */
  90.   unsigned t_cls;     /* priority queue index */
  91.   struct t_task *t_pqtsk,*t_nqtsk; /* queue linkage */
  92.   struct t_task *t_ratsk,*t_pstsk,*t_nstsk,*t_fdtsk,*t_ldtsk; /* family */
  93.   struct sys_parm t_ps;  /* processor status */
  94.   unsigned t_xtm0;     /* execution time accumulator */
  95.   unsigned t_xtm1;
  96.   unsigned t_xtm2;
  97.   pointer *t_axstk;     /* execution stack pointer */
  98. };
  99.  
  100. extern pointer *sys_task; /* current task control table pointer */
  101. #define _tsk ( ( struct t_task * ) sys_task ) /* task control table ref */
  102.  
  103. #define T_SCLS 4     /* number of scheduling classes */
  104.  
  105. struct t_scls         /* scheduling class queue */
  106. {
  107.   unsigned t_sfrq;     /* scheduling frequency */
  108.   int t_sct;         /* queue length */
  109.   struct t_task *t_fqtsk,*t_lqtsk; /* queue header */
  110. };
  111.  
  112. struct t_schd         /* scheduling control table */
  113. {
  114.   int t_xct;         /* execution queue length */
  115.   struct t_task *t_fxtsk, *t_lxtsk; /* execution queue header */
  116.   int t_wct;         /* wait queue length */
  117.   struct t_task *t_fwtsk, *t_lwtsk; /* wait queue header */
  118.   int t_swct;         /* secondary wait queue length */
  119.   struct t_task *t_fswtsk, *t_lswtsk; /* secondary wait queue header */
  120.   int t_sclsl;         /* scheduling class index limit */
  121.   struct t_scls **t_sclsp; /* scheduling class array pointer */
  122. };
  123.  
  124. extern pointer *sys_tsch; /* task scheduling control table pointer */
  125. #define _tschd ( ( struct t_schd * ) sys_tsch ) /* quick pointer */
  126.  
  127. /*
  128. t__krnl          * security kernel *
  129. */
  130. t__krnl()
  131. /*
  132. This is the security kernel.  It never returns, being the most trusted
  133. software in the system. The current contents in t__crtss and t__crtsp
  134. are used to set the stack for when the current task is resumed. */
  135. {
  136.   extern logical t_astrm; /* tick termination flag */
  137.   extern selector t__crtss; /* current task ss storage */
  138.   extern pointer *t__crtsp; /* current task sp storage */
  139.   extern unsigned tmr_tkct; /* tick clock */
  140.   int xtskct;         /* task queue count (at entry) */
  141.   int ttc;         /* task termination code */
  142.   _tsk -> t_ps.sys_ss = t__crtss; /* set current task stack */
  143.   _tsk -> t_ps.sys_sp = t__crtsp;
  144.   while(_T)         /* find executable task */
  145.   {
  146.     xtskct = _tschd -> t_xct; /* save task count */
  147.     if ( t_astrm ) t__wtst( tmr_tkct ); /* process wait tasks */
  148.     if ( xtskct == 0 ) t__sch(); /* schedule application tasks if necessary */
  149.     sys_task = _tschd -> t_fxtsk; /* set next task address */
  150.     if ( sys_task != _NULL )  /* test for executable task available */
  151.     {
  152.       _tschd -> t_xct--; /* decrement executing task count */
  153.       _tschd -> t_fxtsk = _tsk -> t_nqtsk; /* delink task */
  154.       if ( _tschd -> t_fxtsk == _NULL )
  155.     _tschd -> t_lxtsk = _NULL;
  156.       else _tschd -> t_fxtsk -> t_pqtsk = _NULL;
  157.       _tsk -> t_type &= ~T_X; /* indicate task not in execution queue */
  158.       ttc = t__xtsk( sys_task ); /* execute application task */
  159.       if ( !sys_task ) continue; /* test for task terminated */
  160.       if ( ttc < 0 ) t__inxq(); /* insert task into execution queue */
  161.       else if ( ttc == 0 ) t__inpq(); /* insert task into priority queue */
  162.       else t__inwq( ttc ); /* insert into wait queue */
  163.     }
  164.   }
  165. }
  166.  
  167. /*
  168. t__wtst          test waiting tasks
  169. */
  170. t__wtst( tc)
  171. unsigned tc;
  172. /*
  173. The wait queue is traversed.  All tasks with a wait value of tc are executed.
  174. _F is always returned. */
  175. {
  176.   while(_T)         /* traverse wait queue */
  177.   {
  178.     sys_task = _tschd -> t_fwtsk; /* set current task pointer */
  179.     if ( !sys_task ) break; /* test for no waiting tasks */
  180.     _tsk -> t_type &= ~T_W; /* remove task from wait queue */
  181.     _tsk -> t_type |= T_X; /* indicate task in execution queue */
  182.     if ( _tsk -> t_wttk > tc ) break; /* test for effective end of list */
  183.     --_tschd -> t_wct;     /* decrement waiting task  count */
  184.     _tschd -> t_fwtsk = _tsk -> t_nqtsk; /* delink from wait queue */
  185.     if ( _tsk -> t_nqtsk == _NULL )
  186.       _tschd -> t_lwtsk = _NULL;
  187.     else _tsk -> t_nqtsk -> t_pqtsk = _NULL;
  188.     _tsk -> t_pqtsk = _NULL; /* insert at top of execution queue */
  189.     _tsk -> t_nqtsk = _tschd -> t_fxtsk;
  190.     _tschd -> t_fxtsk = sys_task;
  191.     ++_tschd -> t_xct;     /* increment executable task count */
  192.     if ( _tschd -> t_lxtsk == _NULL )
  193.       _tschd -> t_lxtsk = sys_task;
  194.     else _tsk -> t_nqtsk -> t_pqtsk = sys_task;
  195.   }
  196.   return _F;         /* return */
  197. }
  198.  
  199. /*
  200. t__sch             schedule task
  201. */
  202. t__sch()
  203. /*
  204. This function searches the priority queues and links tasks ready for execution
  205. into the execution queue. The return is always _F. */
  206. {
  207.   struct t_scls **a;     /* priority queue pointer array pointer */
  208.   struct t_scls *q;     /* priority queue pointer */
  209.   int i,j;         /* iteration variables */
  210.   a = _tschd -> t_sclsp; /* set pointer array address */
  211. /*  while(_T)            * nonterminating ta